using System;
using System.Collections.Generic;
using Framework;

namespace StateNode
{
    public interface IStateNode
    {
        /// <summary>
        /// 下一阶段的id
        /// </summary>
        protected int NextStateID { get; set; }
        /// <summary>
        /// 这个阶段的id
        /// </summary>
        public int ID { get; protected set; }
        public abstract IStateNode NextID(int id);
        public IStateNode SetID(int id);
        /// <summary>
        /// 拿到下一个阶段的id
        /// </summary>
        /// <returns></returns>
        public int GetNextState();
        /// <summary>
        /// 直接进入下一个阶段
        /// </summary>
        public abstract void ToNextState();
        /// <summary>
        /// 进入该流程会调用一次
        /// </summary>
        public virtual void Enter() { }
        /// <summary>
        /// 离开该流程会调用一次
        /// </summary>
        public virtual void Exit() { }
        public virtual void GameUpdate(float deltaTime) { }

        #region 在这个区间的GameUpdate事件
        public abstract void AddToGameUpdateList(IGameUpdater gameUpdater);
        public abstract void RemoveFromGameUpdateList(IGameUpdater gameUpdater);
        public bool GameUpdaterListContain(IGameUpdater gameUpdater);
        #endregion
    }
    public abstract class AStateNode<T> : GameSingleton<T>, IStateNode where T : Singleton<T>
    {
        int IStateNode.NextStateID
        {
            get { return nextStateID; }
            set { nextStateID = value; }
        }
        private int nextStateID;
        int IStateNode.ID
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }
        private List<IGameUpdater> gameUpdaterList = new List<IGameUpdater>();
        private int id;
        public void AddToGameUpdateList(IGameUpdater gameUpdater)
        {
            if (!gameUpdaterList.Contains(gameUpdater))
                gameUpdaterList.Remove(gameUpdater);
            gameUpdaterList.Add(gameUpdater);
        }
        public void RemoveFromGameUpdateList(IGameUpdater gameUpdater)
        {
            if (!gameUpdaterList.Contains(gameUpdater))
                gameUpdaterList.Remove(gameUpdater);
        }
        public bool GameUpdaterListContain(IGameUpdater gameUpdater)
        {
            return gameUpdaterList.Contains(gameUpdater);
        }

        public IStateNode NextID(int _id)
        {
            nextStateID = _id;
            return this;
        }

        public IStateNode SetID(int _id)
        {
            id = _id;
            return this;
        }
        public void ToNextState()
        {
            ProgressController.Instance.Next();
        }

        public int GetNextState()
        {
            return nextStateID;
        }
    }
}